//
//  ViewController.m
//  ZHIFUBAO
//
//  Created by 高新川 on 15/6/16.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "ViewController.h"
#import "HomeViewController.h"
#import "DiscoveryViewController.h"
#import "AssetsViewController.h"
#import "PublicerviceViewController.h"
#import "UINavigationAssets.h"
#import "UINavigationDiscovery.h"
#import "UINavigationHomeBar.h"
#import "UINavigationPublicService.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 隐藏自带的tabbar
    [self.tabBar setHidden:YES];
    
    // 自定义tabbar
    [self initTabBarIcon];
    [self initTabBarView];
    
    // 初始化切换页
    [self initTabViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initTabBarIcon {
    _tabIcon = [NSArray arrayWithObjects:@"TabBar_HomeBar", @"TabBar_PublicService", @"TabBar_Discovery", @"TabBar_Assets", nil];
    _tabSelIcon = [NSArray arrayWithObjects:@"TabBar_HomeBar_Sel", @"TabBar_PublicService_Sel", @"TabBar_Discovery_Sel", @"TabBar_Assets_Sel", nil];
}

- (void)initTabBarView {
    // 初始化自定义tabbar
    self.tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, 320, 49)];
    //self.tabBarView.layer.borderWidth = 0.1;
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    // 添加四个按钮
    for (int i = 0; i < 4; i++) {
        UIButton *tabButton = [UIButton buttonWithType:UIButtonTypeSystem];
        tabButton.tag = i+1;
        // 设置不同图像
        if (0 == i) {
            [tabButton setImage:[UIImage imageNamed:[_tabSelIcon objectAtIndex:i]] forState:UIControlStateNormal];
        }
        else {
            [tabButton setImage:[UIImage imageNamed:[_tabIcon objectAtIndex:i]] forState:UIControlStateNormal];
        }
        //[tabButton setTitle:@"支付宝" forState:UIControlStateNormal];
        tabButton.frame = CGRectMake(25 + 80 * i, 9, 30, 30);
        [self.tabBarView addSubview:tabButton];
        
        [tabButton addTarget:self action:@selector(tabItemChanged:) forControlEvents:UIControlEventTouchDown];
    }
    
    [self.view addSubview:_tabBarView];
}

- (void)initTabViews {
    NSMutableArray *conArray = [[NSMutableArray alloc] init];
    
    // 初始化支付宝页面
    HomeViewController *homeCon = [[HomeViewController alloc] init];
    UINavigationHomeBar *homeNavigation = [[UINavigationHomeBar alloc] initWithRootViewController:homeCon];
    [conArray addObject:homeNavigation];
    
    // 初始化服务窗页面
    PublicerviceViewController *serviceCon = [[PublicerviceViewController alloc] init];
    UINavigationPublicService *serviceNavigation = [[UINavigationPublicService alloc] initWithRootViewController:serviceCon];
    [conArray addObject:serviceNavigation];
    
    // 初始化服务窗页面
    DiscoveryViewController *disCon = [[DiscoveryViewController alloc] init];
    UINavigationDiscovery *disNavigation = [[UINavigationDiscovery alloc] initWithRootViewController:disCon];
    [conArray addObject:disNavigation];
    
    // 初始化服务窗页面
    AssetsViewController *assetsCon = [[AssetsViewController alloc] init];
    UINavigationAssets *assNavigation = [[UINavigationAssets alloc] initWithRootViewController:assetsCon];
    [conArray addObject:assNavigation];
    
    self.viewControllers = conArray;
}

- (void)tabItemChanged:(UIButton *)button {
    self.selectedIndex = button.tag - 1;    // 设置不同页面
    // 更新图片
    for (int i = 0; i < 4; i++) {
        UIButton *but = (UIButton *)[self.view viewWithTag:(i+1)];
        if (button.tag == i+1) {
            [but setImage:[UIImage imageNamed:[_tabSelIcon objectAtIndex:i]] forState:UIControlStateNormal];
        }
        else {
            [but setImage:[UIImage imageNamed:[_tabIcon objectAtIndex:i]] forState:UIControlStateNormal];
        }
    }
}

@end
